import { usePublicHooks } from "../../../hooks";
import { usePublicFormats } from "../../../formats";

import { reactive, onMounted, h, toRaw } from "vue";
import { delay } from "@pureadmin/utils";
import { addDialog } from "@/components/ReDialog";
import { message } from "@/utils/message";
import editForm from "../form.vue";
import type { FormItemProps } from "./types";
import { entryList, entryAdd } from "@/api/artificial";

export function artificialEntryConfig() {
  const form = reactive({
    car_number: "",
    car_no_color: -1,
    park_code: "",
    park_name: "",
    much_id: "",
    in_time: "",
    page: 1,
    limit: 20,
    is_admin: 1
  });

  //从父级钩子里取东西
  const { pagination, loadingConfig, dataList, formRef, loading } =
    usePublicHooks();

  const { carColorShow, carTypeShow } = usePublicFormats();

  const columns: TableColumnList = [
    {
      label: "车牌号",
      prop: "car_number",
      fixed: true
    },
    {
      label: "车牌颜色",
      prop: "car_no_color",
      cellRenderer: ({ row }) => {
        return carColorShow(row.car_no_color);
      }
    },

    {
      label: "停车场名称",
      prop: "park_name"
    },
    {
      label: "入场时间",
      prop: "in_time"
    },
    {
      label: "订单号",
      prop: "order_no"
    },
    {
      label: "商户编号",
      prop: "much_id"
    }
  ];

  async function onSearch() {
    loading.value = true;
    const { data } = await entryList(toRaw(form));
    dataList.value = data.list;
    pagination.total = data.total;
    pagination.pageSize = data.pageSize;
    pagination.currentPage = data.currentPage;

    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  //页面加载
  onMounted(() => {
    onSearch();
  });

  //页面条数变化
  function onSizeChange(val) {
    form.limit = val;
    onSearch();
  }

  //页码变化
  function onCurrentChange(val) {
    loadingConfig.text = `正在加载第${val}页...`;
    loading.value = true;
    delay(600).then(() => {
      form.page = val;
      onSearch();

      //刷新数据
      loading.value = false;
    });
  }

  //重置表单
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }

  function openDialog(title = "新增", row?: FormItemProps) {
    addDialog({
      width: "30%",
      title: "新增出场记录",
      contentRenderer: () => h(editForm, { ref: formRef }),
      props: {
        formInline: {
          car_number: row?.car_number ?? "",
          car_no_color: row?.car_no_color ?? -1,
          park_code: row?.park_code ?? "",
          much_id: row?.much_id ?? "",
          in_time: row?.in_time ?? ""
        }
      },
      beforeSure: (done, { options }) => {
        const FormRef = formRef.value.getRef();
        const curData = options.props.formInline as FormItemProps;
        function chores() {
          message(`您${title}了名称为${curData.car_number}的这条数据`, {
            type: "success"
          });
          done(); // 关闭弹框

          setTimeout(function () {
            onSearch(); // 延时一秒后刷新表格数据
          }, 1000); // 1000毫秒 = 1秒
        }
        FormRef.validate(valid => {
          if (valid) {
            // 表单规则校验通过
            if (title === "新增") {
              entryAdd(curData).then(() => {
                // 实际开发先调用新增接口，再进行下面操作
                chores();
              });
            }
          }
        });
      }
    });
  }

  return {
    columns,
    form,
    loading,
    onSearch,
    resetForm,
    dataList,
    pagination,
    onSizeChange,
    onCurrentChange,
    loadingConfig,
    openDialog
  };
}
